package com.ruix.app.mapper;

import com.ruix.app.manager.UserManager;
import com.ruix.app.model.Classes;
import com.ruix.app.model.PostsCard;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by hadexs on 2017/4/19.
 */

public class PostsCardMapper implements ApiMapper<ArrayList<PostsCard>> {


    @Override
    public ArrayList<PostsCard> transform(JSONObject response) {
        ArrayList posts  = new ArrayList();
        JSONObject data = response.optJSONObject("data");
        if (data != null) {
            JSONArray postList = data.optJSONArray("post_list");
            if (postList != null) {
                for (int i = 0; i < postList.length(); i++){
                    JSONObject object = postList.optJSONObject(i);
                    PostsCard postsCard = new PostsCard().parse(object);
                    posts.add(postsCard);
                }
            }
            JSONObject classObj = data.optJSONObject("class");
            Classes classes = new Classes().parse(classObj);
            UserManager.sharedInstance().updateCurrentClassState(classes);
        }
        return posts;
    }
}
